<template>
  <div class="searchBox">
    <el-card class="box-card">
      <div class="top">
        <div class="top_left">
          <el-icon><Search /></el-icon>
          <span class="search_txt">筛选搜索 </span>
        </div>
        <div class="top_right">
          <el-button type="default" @click="reset">重置</el-button>
          <el-button type="primary" @click="searchParamsFn">查询搜索</el-button>
        </div>
      </div>
      <div class="under">
        <span>活动名称： </span>
        <el-input size="small" placeholder="活动名称" v-model="form.keyword" />
      </div>
    </el-card>
  </div>
  <div class="add">
    <el-card>
      <el-icon><Tickets /></el-icon>数据列表
      <div class="btn">
        <el-button @click="router.push('/sms/flashSession')"
          >秒杀时间段列表</el-button
        >
        <el-button @click="showDialog = true">添加活动</el-button>
      </div>
    </el-card>
  </div>
  <el-table
    :data="list"
    border
    table-layout="auto"
    cell-class-name="align-c"
    header-row-class-name="align-c"
  >
    <el-table-column type="selection" width="55" />
    <el-table-column label="编号" prop="id" />
    <el-table-column label="活动标题" prop="title" />
    <el-table-column label="活动状态">
      <span>活动未开始</span>
    </el-table-column>
    <el-table-column label="开始时间" prop="startDate">
      <template #default="{ row }">
        <span>{{ dateFormat(row.startDate) }}</span></template
      ></el-table-column
    >
    <el-table-column label="结束时间" prop="endDate">
      <template #default="{ row }">
        <span>{{ dateFormat(row.endDate) }}</span></template
      >
    </el-table-column>
    <el-table-column label="上线/下线" prop="loginTime">
      <template #default="{ row }">
        <el-switch
          v-model="row.status"
          :active-value="1"
          :inactive-value="0"
          @change="updateStatus(row)"
        />
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button
          text
          type="primary"
          v-permission="'selectSession'"
          size="small"
          @click="setSession(row.id)"
          >设置商品</el-button
        >
        <el-button text type="primary" size="small" @click="editFlash(row)"
          >编辑</el-button
        >
        <el-button text type="primary" size="small" @click="del(row.id)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <div class="page">
    <el-config-provider :locale="locale">
      <el-pagination
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="pageChangeHandle"
        @current-change="pageSizeChangeHandle"
    /></el-config-provider>
  </div>
  <editFlashVue
    :show="showDialog"
    @hide="closeDialog"
    :id="editId"
    :title="title"
    :startDate="startDate"
    :endDate="endDate"
    :status="status"
  ></editFlashVue>
</template>
<script setup lang="ts">
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { Search, Tickets } from "@element-plus/icons-vue";
import { dateFormat } from "@/utils/index";
import { reactive, ref } from "vue";
import useTable from "@/hooks/useTable";
import { deleteFlashApi, getFlashListApi, updateFlashStatusApi } from "./api";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
import editFlashVue from "./editFlash.vue";
const locale = zhCn;
const router = useRouter();
const editId = ref<number | undefined>(0);
const title = ref("");
const startDate = ref("");
const endDate = ref("");
const status = ref(0);
const form = reactive({
  keyword: "",
});
const {
  list,
  pageNum,
  total,
  pageChangeHandle,
  pageSizeChangeHandle,
  pageSize,
  searchParamsFn,
} = useTable(getFlashListApi, form);
// 弹出框显隐
const showDialog = ref(false);
// 关闭弹出框
const closeDialog = (isReload?: boolean) => {
  showDialog.value = false;
  if (isReload) {
    searchParamsFn();
  }
};
// 重置
const reset = () => {
  form.keyword = "";
  searchParamsFn();
};
// 切换状态
const updateStatus = (row: IFlashList) => {
  updateFlashStatusApi(row.id!, row.status)
    .then((res) => {
      if (res.code === 200) {
        // console.log(res);
        ElMessage.success("修改成功");
      } else {
        row.status = row.status === 1 ? 0 : 1;
      }
    })
    .catch(() => {
      row.status = row.status === 1 ? 0 : 1;
    });
};
// 设置商品
const setSession = (id: number) => {
  console.log(id);
  router.push(`/sms/selectSession?flashPromotionId=${id}`);
};
// 编辑
const editFlash = (row: IFlashList) => {
  showDialog.value = true;
  editId.value = row.id;
  title.value = row.title;
  startDate.value = row.startDate;
  endDate.value = row.endDate;
  status.value = row.status;
};
// 删除
const del = (id: number) => {
  ElMessageBox.confirm("确定要删除吗？", "确定删除", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  }).then(() => {
    deleteFlashApi(id).then((res) => {
      if (res.code === 200) {
        ElMessage.success("删除成功");
        searchParamsFn();
      }
    });
  });
};
</script>
<style lang="less" scoped>
.searchBox {
  :deep(.el-card) {
    padding: 20px;
  }
  .top {
    position: relative;
    display: flex;
    .top_left {
      .search_txt {
        padding-left: 10px;
        font-size: 14px;
      }
    }
    .top_right {
      position: absolute;
      right: 20px;
    }
  }
  .under {
    padding: 20px 40px 0;
    font-size: 14px;
    :deep(.el-input) {
      height: 32px;
      line-height: 32px;
      width: 210px;
    }
  }
}
.add {
  margin: 20px 0;
  :deep(.el-card__body) {
    display: flex;
    position: relative;
    // flex-wrap: wrap;
    // justify-content: space-between;
    align-items: center;
  }
  .btn {
    position: absolute;
    right: 10px;
  }
}
:deep(.align-c) {
  .cell {
    text-align: center !important;
    font-size: 12px;
  }
}
.page {
  display: flex;
  justify-content: flex-end;
}
</style>
